#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time
import urllib.request
import json
import requests
from openpyxl import Workbook

headers=("user-agent","ting_6.3.93(BKL-AL20,Android26)")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
urllib.request.install_opener(opener)
#调取“个人提升”--“最新更新”下面的内容

# 根据节目id获取评论总页数
def get_totalPage_comment(ts, albumId):
    url = "http://180.153.255.6/comment-mobile/album/ts-"+ts+"?albumId="+albumId+"&pageId={}&pageSize=1"
    cookies = {}
    # 每次运行程序，这两个cookies都要重新设置。在filder里的header 下的cookies里
    cookies["1&_token"] = "14257703&c78c12fd0ad99046c90a4690c2f7954b2934"
    cookies["1&_device"] = "android&00000000-36a7-d3f5-0000-0000003652ee&6.3.93"
    response = requests.get(url.replace("{}", str(1)), cookies=cookies)
    response.encoding = "utf-8"
    josn_text = response.text
    result_dicts = json.loads(josn_text)
    return result_dicts["maxPageId"]

def get_comments_info(ts,albumId,page,work_book):
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    print(str(otherStyleTime) + " get comments info ")
    url = "http://180.153.255.6/comment-mobile/album/ts-"+ts+"?albumId="+albumId+"&pageId={}&pageSize=20"
    cookies = {}
    # 每次运行程序，这两个cookies都要重新设置。在filder里的header 下的cookies里
    cookies["1&_token"] = "14257703&c78c12fd0ad99046c90a4690c2f7954b2934"
    cookies["1&_device"] = "android&00000000-36a7-d3f5-0000-0000003652ee&6.3.93"
    response = requests.get(url.replace("{}", str(page)), cookies=cookies)
    response.encoding = "utf-8"
    josn_text = response.text
    result_dicts = json.loads(josn_text)
    result_lists = result_dicts["list"]
    ws = work_book.active
    for result_list in result_lists:
        comments = []
        comments.append(result_list["album_id"])
        comments.append(result_list["content"])
        comments.append(result_list["album_content_score"])
        comments.append(result_list["album_score"])
        comments.append(result_list["album_user_score"])
        comments.append(result_list["created_at"])
        comments.append(result_list["isVip"])
        comments.append(result_list["liked"])
        comments.append(result_list["likes"])
        comments.append(result_list["nickname"])
        comments.append(result_list["replyCount"])
        if "album_comment_tags" in result_list.keys():
            comments.append(result_list["album_comment_tags"])
        ws.append(comments)
    return work_book

# 把评论信息写入文件
def write_comments_excel(ts, albumId):
    work_book = Workbook()
    totalPage = get_totalPage_comment(ts, albumId)
    for page in range(totalPage):
        time.sleep(1)  # 让程序停顿1秒
        work_book = get_comments_info(ts, albumId, page, work_book)
    work_book.save(str(albumId)+"comments.xlsx")
    logTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    print(str(logTime)+"write comments finish")

# 获取节目列表的总页数
def get_page_num():
    url = "http://180.153.255.6/product/v1/category/metadata/albums/ts-" \
          "1525185063292?calcDimension=recent&categoryId=54&device=" \
          "android&pageId={}&pageSize=1&version=6.3.93"
    response = requests.get(url.replace("{}", str(1)))
    response.encoding = "utf-8"
    josn_text = response.text
    result_dicts = json.loads(josn_text)
    print(isinstance(result_dicts, tuple))
    print(result_dicts)
    # 返回总页数
    return result_dicts["maxPageId"]

# 根据页数获取节目列表信息
def get_data_info(page, work_book):
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    print(str(otherStyleTime) + " get program info ")
    url="http://180.153.255.6/product/v1/category/metadata/albums/ts-" \
        "1525185063292?calcDimension=recent&categoryId=54&device=" \
        "android&pageId={}&pageSize=50&version=6.3.93"
    response = requests.get(url.replace("{}", str(page)))
    response.encoding = "utf-8"
    josn_text = response.text
    result_dicts = json.loads(josn_text)
    result_lists = result_dicts["list"]
    # print(result_lists)
    ws = work_book.active
    for result_list in result_lists:
        # for key,value in result_list.item():
        #     print(key,value)
        program = []  # 定义一个数组
        program.append(result_list["id"])
        program.append(result_list["commentsCount"])
        program.append(result_list["title"])
        program.append(result_list["price"])
        program.append(result_list["intro"])
        program.append(result_list["score"])
        program.append(result_list["playsCounts"])
        try:
            write_comments_excel("1525185063292", str(result_list["albumId"]))
        except BaseException:
            print("write comments to excel exception")
        ws.append(program)  # 二维数组
    return work_book

#把节目列表信息写入文件
def writeExcel(totalPage):
    work_book = Workbook()
    for page in range(totalPage):
        if(page == 2):
            break
        time.sleep(1)  # 让程序停顿1秒
        work_book = get_data_info(page, work_book)
    work_book.save("data.xlsx")

#程序入口
if __name__ == '__main__':
    # 获取总页数
    totalPage = get_page_num()
    print(totalPage)
    writeExcel(totalPage)
    # totalPage = get_totalPage_comment("1526823718119", "14624481")
    # print(totalPage)
    # write_comments_excel("1526823718119", "14624481")
'''
url2="http://180.153.255.6/product/v1/category/metadata/albums/ts-" \
     "1525185731540?calcDimension=recent&categoryId=51&device" \
     "=android&pageId=1&pageSize=20&version=6.3.93"
url3="http://180.153.255.6/product/v1/category/metadata/albums/ts-" \
     "1525185869562?calcDimension=recent&categoryId=52&device=" \
     "android&pageId=1&pageSize=20&version=6.3.93"
url4="http://180.153.255.6/product/v1/category/metadata/albums/ts-" \
     "1525185991956?calcDimension=recent&categoryId=47&device=" \
     "android&pageId=1&pageSize=20&version=6.3.93"
'''
